"use strict";
/**
 * @name help
 * 好友助战
 */


/**
 * @name setHelp
 * @param string unitID  助战单位ID
 * @param int siteID 助战单位位置
 * 设置玩家助战单位
 */
exports.setHelp = async function () {
    const id = this.get('unitID', 'string');
    const siteID = this.get('siteID', 'int');
    if (siteID < 0 || siteID > 2) {
        return this.quick_error("M2saghge20", "位置错误", siteID);
    }
    let unitMsg = {};
    if (id != '') {
        console.log("设置玩家助战单位", id);
        this.updater.item.key(id);
        this.updater.role.key('helpUnit');
        await this.updater.data();
        let unitData = this.updater.item.get(id);
        if (!unitData) {
            return this.quick_error("M2saghge20", "单位不存在", id);
        }
        let helpUnit = this.updater.role.get('helpUnit');
        for (let key in helpUnit) {
            if (helpUnit[key]._id == id) {
                return this.quick_error("M2saghge20", "单位已经设置过了", id);
            }
        }
        unitMsg['_id'] = unitData._id
        unitMsg['id'] = unitData.id
        unitMsg['lv'] = unitData.lv
        unitMsg['slv'] = unitData.slv
    }
    this.updater.role.set('helpUnit.' + siteID, unitMsg);
    return this.updater.save();
};


/**
 * @name helpList
 * @param string key 额外字段,多个以","分割
 * 助战列表
 */
exports.helpList = async function () {
    let num = 100;
    let key = this.get('key', 'string') || '';
    // for (let k of arr_key) {
    //     if (k) fk.push(k);
    // }
    let query = { "uid": this.uid, "val": 1 }
    let option = { "multi": true, "dataType": "array", "fields": { "_id": -1, "fid": 1 } };
    let friend = this.model.mongo("friend", this.sid);
    let friendData = await friend.find(query, option);


    let arr = [];
    if (friendData.length != 0) {
        for (let d of friendData) {
            arr.push(d.fid);
        }
    }
    // let key = 'name,helpUnit'
    let role = this.model.mongo("role", this.sid);
    let fields = { "_id": 1 };
    let time = new Date().getTime() - 7 * 24 * 60 * 60 * 1000;
    //筛选已经是好友的uid
    arr.push(this.updater.uid);
    let query2 = { "login": { "$gt": time }, "_id": { "$ne": arr }, "helpUnit": { "$ne": { "0": {}, "1": {}, "2": {} } } };
    if (key) {
        let arrKey = key.split(",");
        for (let k of arrKey) {
            fields[k] = 1;
        }
    }
    let helpData = await role.aggregate([
        { $sample: { size: num } },
        { $match: query2 },
        { $project: fields }
    ], {})

    let helpNum = 0;
    for (let k in helpData) {
        helpNum++;
    }
    if (helpNum < 5) {
        let query2 = { "_id": { "$ne": arr }, "helpUnit": { "$ne": { "0": {}, "1": {}, "2": {} } } };
        let helpData2 = await role.aggregate([
            { $sample: { size: 50 } },
            { $match: query2 },
            { $project: fields }
        ], {})
        for (let k in helpData2) {
            if (!helpData.hasOwnProperty(k)) {
                helpData[k] = helpData2[k];
            }
        }
        return helpData;
    } else {
        return helpData;
    }
}

/**
 * @name usehelp
 * @param string uid 角色唯一ID
 * 使用助战
 */
exports.usehelp = async function () {
    const id = this.get('uid', 'string');
    this.updater.daily.key('useHelp');
    await this.updater.data();
    let useList = this.updater.daily.get('useHelp') || [];
    if (!useList) {
        return this.quick_error("M2saghge20", "找不到数据", uid);
    }
    for (let i = 0; i < useList.length; i++) {
        if (useList[i] == id) {
            return this.quick_error("M2saghge20", "单位已经被使用过了", id);
        }
    }
    const assistCount = this.config('base', 'assistCount').val;
    if (useList.length >= assistCount) {
        return this.quick_error("M2saghge20", "今日助战已达最大次数", id);

    }
    useList.push(id);
    this.updater.daily.set('useHelp', useList);
    this.updater.daily.add('record.' + 37, 1);
    this.updater.record.add(37, 1);
    return this.updater.save();
}
